using System.Collections;
using System.Collections.Generic;
using BehaviorDesigner.Runtime.Tasks;
using DG.Tweening;
using UnityEngine;

public class Patrol : EnemyAction
{
    public Vector3 endPos;
    
    bool isProtal;
    private Vector2 dir;
    private float speed;
    
    private Vector3 startPos;
    
    public bool canWait;
    private float canWaitTime;
    
    public override void OnStart()
    {
        startPos = transform.position;
        dir = (endPos - startPos).normalized;
        transform.localScale = new Vector3(dir.x > 0 ? 1 : -1, 1, 1);
        speed = Settings.protalSpeed;
        canWaitTime = Settings.protalWaitCount;
        isProtal = false;
    }

    public override TaskStatus OnUpdate()
    {
        if (Mathf.Abs(transform.position.x -  endPos.x) <= 0.1f)
        {
            isProtal = true;
            rb.velocity = Vector2.zero;
            if (canWaitTime <= 0) return TaskStatus.Success;
            canWaitTime -= Time.deltaTime;
        }
        
        return TaskStatus.Running;
    }

    public override void OnFixedUpdate()
    {
        if(!isProtal) Move();
    }
    
    protected void Move()
    {
        rb.velocity = new Vector2(dir.x * speed * Time.deltaTime, rb.velocity.y);
    }
    
}
